package com.zqq.demo.fragment

import android.os.Build
import android.webkit.WebSettings
import com.zqq.base.BaseParentFragment
import com.zqq.base.utils.LogUtils
import com.zqq.demo.R
import com.zqq.demo.databinding.FragmentCBinding
import com.zqq.demo.viewmodel.PViewModel


class CFragment: BaseParentFragment<PViewModel, FragmentCBinding>() {
    override fun setContentViewId(): Int = R.layout.fragment_c

    override fun init() {
        viewModel.url.observe(this) {
            LogUtils.i("zqq_demo","url>>>${it}")
            loadUrl(it)
        }
    }

    private fun loadUrl(it: String) {
        val webSetting: WebSettings = viewBinding.webview.getSettings()
        // 设置允许访问文件数据
        webSetting.allowFileAccess = true
        webSetting.allowContentAccess = true
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        val ua: String = viewBinding.webview.getSettings().getUserAgentString()
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING
        webSetting.setSupportZoom(true)
        webSetting.builtInZoomControls = true
        webSetting.displayZoomControls = false
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(true)

        //        webSetting.setAppCacheEnabled(true);
        webSetting.domStorageEnabled = true
        webSetting.setGeolocationEnabled(true)

        //        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.mediaPlaybackRequiresUserGesture = false
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        webSetting.cacheMode = WebSettings.LOAD_DEFAULT
        webSetting.loadWithOverviewMode = true
        webSetting.savePassword = true
        webSetting.saveFormData = true
        webSetting.javaScriptEnabled = true


        //        webSetting.setTextSize(WebSettings.TextSize.LARGEST);
//        webSetting.setDefaultFontSize(DpOrPxUtils.sp2px(this,16));
        webSetting.pluginState = WebSettings.PluginState.ON
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webSetting.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        }

        viewBinding.webview.loadUrl(it)
    }
}